package cn.com.klun.tjy.other.zkUsed;

import cn.com.klun.tjy.other.util.ExecutorServiceUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.Date;
import java.util.concurrent.ExecutorService;

public class zkTest {
    private static Logger logger = LoggerFactory.getLogger(zkTest.class);
    public static void main(String[] args) {
        //开启一个线程做 计时任务
        ExecutorService executorService = ExecutorServiceUtil.newExecutorInstance();

        // 建立 master 任务分配和调度 只有一个会注册成功
        Master master0 = new Master("192.168.137.11:2181");
        //Master master1 = new Master("192.168.137.12:2181");
        //Master master2 = new Master("192.168.137.13:2181");
        // 注册workers
        Worker worker0 = new Worker("192.168.137.11:2181");
        Worker worker1 = new Worker("192.168.137.11:2181");
        Worker worker2 = new Worker("192.168.137.11:2181");
        Worker worker3 = new Worker("192.168.137.11:2181");
        Worker worker4 = new Worker("192.168.137.11:2181");
        // 客户端角色 可以多个 可以一个   注册任务用
        Client cient0 = new Client("192.168.137.12:2181");

        try {
            executorService.submit(new Runnable() {
                @Override
                public void run() {
                    long l = System.currentTimeMillis() + 1000*5*60;
                    System.out.println("下次执行时间"+new Date(l));
                    int count = 0;
                    while(true){
                        try {
                            Thread.sleep(10000);
                            if(System.currentTimeMillis() > l){
                                l =  System.currentTimeMillis() +1000*5*60;
                                if(count == 0){
                                    //master0.stopZk();
                                }
                                if(count == 1){worker0.stopZK();}
                                if(count == 2){
                                    master0.stopZk();
                                    //master1.stopZk();
                                }
                                if(count == 3){worker1.stopZK();}
                                if(count == 4){worker3.stopZK();
                                    try {
                                        cient0.queueCommand(2+"");
                                    } catch (Exception e) {
                                        e.printStackTrace();
                                    }
                                }
                                count ++;
                            }
                        } catch (InterruptedException e) {}
                    }
                }
            });
            // 建立 master 任务分配和调度
//            master2.startZk();
//            master2.runForMaster();//serverId
//            master1.startZk();
//            master1.runForMaster();//serverId
            master0.startZk();
            master0.runForMaster();//serverId

            // 建立 worker 执行任务
            worker0.startZk();
            worker0.register();
            worker0.setStatus("1");

            worker1.startZk();
            worker1.register();
            worker1.setStatus("2");

            worker2.startZk();
            worker2.register();
            worker2.setStatus("3");

            worker3.startZk();
            worker3.register();
            worker3.setStatus("2");
//            worker4.startZk();
//            worker4.register();
//            worker4.setStatus("1");

            // 客户端注册任务
            cient0.startZk();
            String name  = cient0.queueCommand(1+""); //发布一个任务
            logger.info("Create"+name);

            // 最后睡一定的时间保证存活一段时间
            Thread.sleep(600000);
        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            try {
                master0.stopZk();
//                master1.stopZk();
//                master2.stopZk();
                worker0.stopZK();
                worker1.stopZK();
                worker2.stopZK();
                worker3.stopZK();
                worker4.stopZK();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }
}
